//
//  leibieCell.m
//  HAODE
//
//  Created by panquanReabam.com on 15/11/9.
//  Copyright © 2015年 reabam.com. All rights reserved.
//

#import "leibieCell.h"

//
//  PlatformProductTypeListCell.m
//  HAODE
//
//  Created by panquanReabam.com on 15/11/2.
//  Copyright © 2015年 reabam.com. All rights reserved.
//



#import "UIImageView+WebCache.h"
#import "UIButton+WebCache.h"
#import "HttpRequest.h"
#import "HAODEsingleton.h"
#define  lineColor  [UIColor colorWithRed:228.00/255.0 green:228.00/255.0 blue:228.00/255.0 alpha:1]

#import "UIViewExt.h"
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth  [UIScreen mainScreen].bounds.size.width

@interface leibieCell ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    NSArray *array;
    NSInteger collectionHeight;
    UIView *containerView;
    UICollectionView *collection;
    HAODEsingleton*commdity;
      NSMutableArray *selectFeatureCodeArray;
    NSMutableArray *selectFeatureCode;
}
@end
@implementation leibieCell{
    
    
}

- (void)awakeFromNib {
    selectFeatureCodeArray=[[NSMutableArray alloc]init];
    selectFeatureCode=[[NSMutableArray alloc]init];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _imageArrayname.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"collection";
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:str forIndexPath:indexPath];
    for (UIView *view in [cell.contentView subviews]) {
        [view removeFromSuperview];
    }
    
    UIButton* image=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, cell.width, cell.height)];
    image .titleLabel.font=[UIFont systemFontOfSize:14];
    image.layer.borderWidth = 0.35;
    image.layer.borderColor = qianhuicolor.CGColor;
    [image setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];

    if  ([selectFeatureCodeArray containsObject:[_imageArrayname objectAtIndex:indexPath.item]]){
        

        [image setBackgroundImage:[UIImage imageNamed:@"gouxuan"] forState:UIControlStateNormal];
//        _saveAccount=NO;
        
    }
    
    
    image.titleLabel.textAlignment=NSTextAlignmentCenter;
    [image setTitle:_imageArrayname[indexPath.row] forState:UIControlStateNormal];
    [image setTitleColor:zhonghuiColor forState:UIControlStateNormal];
    image.userInteractionEnabled=NO;
    
    [cell addSubview:image];
    
    return cell;
}

#define kYOfImageContainer   136.0

- (void)setComment:(MCirclemodel *)comment
{
    
    commdity =[HAODEsingleton shared];
    _comment = comment;
    _imageArrayname= comment.imagePicBig;
    _imageArraycode= comment.imagePicSmall;
//    NSLog(@"[[[[[[%@",selectFeatureCodeArray);
    
    self.diselabelview.layer.borderWidth = 0.35;
    self.diselabelview.layer.borderColor = qianhuicolor.CGColor;
    self.nameLabel.text = comment.userName;
    [collection removeFromSuperview];
    
    UILabel *quan = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 15, 15)];
    quan.textColor = luseColor;
    quan.text=@"o";
    quan.font=[UIFont boldSystemFontOfSize:15];
    [self.contentView addSubview:quan];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake((ScreenWidth-45)/4 , 30);
    
    
    layout.minimumInteritemSpacing = 1;
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    
    UIImageView *_xianview = [[UIImageView alloc]initWithFrame:CGRectMake(15,50-0.5, ScreenWidth-30, 0.5)];
    _xianview.image=[UIImage imageNamed:@"hengxian"];
    
    [self.contentView addSubview:_xianview];
    
    [collection removeFromSuperview];
    
    
    collectionHeight = (comment.imagePicSmall.count/5 +1) *(layout.itemSize.width + 10) -10;
    
    collection = [[UICollectionView alloc]initWithFrame:CGRectMake(12.5, 60, ScreenWidth-25, collectionHeight) collectionViewLayout:layout];
    collection.backgroundColor = [UIColor clearColor];
//    collection.alwaysBounceVertical = YES;
    [collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"collection"];
    collection.delegate = self;
    collection.dataSource = self;
    
    
    //    collection.userInteractionEnabled=NO;
    [self.contentView addSubview:collection];
    
    
}

+ (CGFloat)cellHeight:(MCirclemodel *)commentModel
{
    // 手动计算一下containerView的高度，拿此高度再加其它控件的高度
    
    
    
    CGFloat headImageHeight = 60;
    
    //collectionView height
    NSInteger arrayCount = commentModel.imagePicSmall.count;
    
    CGFloat collectionH = (arrayCount/5 +1)* 30;
    
    return  headImageHeight  + collectionH+20;
    
}



- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    _saveAccount=! _saveAccount;
    
         
    if (!_saveAccount) {
        
        
        for (int i=0;  i<_imageArrayname.count; i++) {

            UICollectionViewCell* cell1 = [collectionView cellForItemAtIndexPath:indexPath];
            cell1.layer.borderWidth = 0.35;
            cell1.layer.borderColor = qianhuicolor.CGColor;
            
            if (selectFeatureCodeArray.count>0) {
                
                            for (int i=0; i<selectFeatureCodeArray.count; i++) {
                
                                NSString *codeStr = [selectFeatureCodeArray objectAtIndex:i];
                                if ([codeStr isEqualToString:[_imageArrayname objectAtIndex:indexPath.item]] ) {
                
                                    [selectFeatureCodeArray removeObjectAtIndex:i];//从指定索引移除
                                    [selectFeatureCode removeObjectAtIndex:i];//从指定索引移除
                                }
                            }
            }

        }
          NSLog(@"11111=========%ld",indexPath.item);
        commdity.leibeicontent=selectFeatureCodeArray;
        commdity.leibeicode=selectFeatureCode;
     }else{
        UICollectionViewCell* cell1 = [collectionView cellForItemAtIndexPath:indexPath];
        cell1.layer.borderWidth = 0.35;
        cell1.layer.borderColor = luseColor.CGColor;
       [selectFeatureCodeArray addObject:[_imageArrayname objectAtIndex:indexPath.item]];
       [selectFeatureCode addObject:[_imageArraycode objectAtIndex:indexPath.item]];
     
         commdity.leibeicontent=selectFeatureCodeArray;
         commdity.leibeicode=selectFeatureCode;
         NSLog(@"11111=========%ld",indexPath.item);
     }
    
    
   
    
    NSLog(@"11111=========%@",commdity.leibeicode);
    NSLog(@"22222---------%@",commdity.leibeicontent);

    [_mDelegate tiaojiemian1:selectFeatureCode];
    
    
     
    
}
- (void)collectionView:(UICollectionView *)collectionView didHighlightItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
}
- (void)collectionView:(UICollectionView *)colView  didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    
    
}











@end